package com.paginasenlanube.service;

import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;



import org.hibernate.SessionFactory;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.paginasenlanube.dao.GenericDao;
import com.paginasenlanube.dao.PageDaoImpl;
import com.paginasenlanube.dao.WebsiteDaoImpl;
import com.paginasenlanube.model.Design;

import com.paginasenlanube.model.Page;
import com.paginasenlanube.model.SocialFeed;
import com.paginasenlanube.model.Website;

import com.paginasenlanube.utils.Utils;
import com.paginasenlanube.view.Config;


@Service
public class SiteConfigServiceImpl implements SiteConfigService{

	  
	  @Autowired
	    private SessionFactory sessionFactory;
	     	  
			    
		public SessionFactory getSessionFactory() {
			return sessionFactory;
		}
		
		 @Autowired
		 @Qualifier("hibernateDao")
		 private GenericDao dao;
		  
		 public GenericDao getDao() {
			return dao;
		 }
			
		@Autowired
		@Qualifier("websiteDao")
		private WebsiteDaoImpl websiteDAO;
		
		public WebsiteDaoImpl getWebsiteDAO() {
			return websiteDAO;
		}

		@Autowired
		@Qualifier("pageDao")
		private PageDaoImpl pageDAO;
		
		public PageDaoImpl getPageDAO() {
			return pageDAO;
		}

		@Resource(name = "myProps")
	    private Properties myProps;
		

		@Autowired
		   private Website website;

		@Autowired
		   private Config cfg;
	
	@Override
	@Transactional
	public String addPage(String jsonString) {
		Page p = null;
		JsonArray json = Utils.getJsonArray(jsonString);
		for(int j = 0;j<json.size();j++){
			
		String pageName = json.get(j).getAsJsonObject().get("name").getAsString();
		boolean showPage = json.get(j).getAsJsonObject().get("showPage").getAsBoolean();
		int pagePosition = json.get(j).getAsJsonObject().get("position").getAsInt();
		String pageType = json.get(j).getAsJsonObject().get("pageType").getAsString(); 
		long menu_id = website.getMenu().getId();
	    String template ="";
	    	
			 p = new Page(pageName,template,pagePosition,showPage,menu_id);
			 p.setPageType(pageType);
			 dao.add(p);
			 
		}
		Map<String, String> data = new HashMap<String, String>();
		data.put("id", String.valueOf(p.getId()));
		data.put("name", p.getName());
		 
		//init Gson
		Gson gson = new Gson();
		 
		
		return gson.toJson(data);
		
	}

	@Override
	@Transactional
	public void updatePage(String jsonString) {
		JsonArray json = Utils.getJsonArray(jsonString);
		for(int j = 0;j<json.size();j++){
			long pageId = Long.parseLong(json.get(j).getAsJsonObject().get("id").getAsString().substring(4));
			String pageName = json.get(j).getAsJsonObject().get("name").getAsString();
			boolean showPage = json.get(j).getAsJsonObject().get("showPage").getAsBoolean();
			int pagePosition = json.get(j).getAsJsonObject().get("position").getAsInt();
		
			//Session s = sessionFactory.getCurrentSession();
			//Page p = (Page) s.load(Page.class, new Long(pageId));
			Page p = dao.getObjectById(Page.class,pageId);
			pageDAO.updatePage(p,pageName,showPage,pagePosition);	
			
		}
		
	}

	
	@Override
	@Transactional
	public List<Website> getWebsitesByUserId(long userId) {
		List<Website> wbs = websiteDAO.getWebsitesByUserId(userId);	
		return wbs;
		
	}

	@Override
	@Transactional
	public List<Page> getPages(long menuId) {
		List<Page> pages = pageDAO.getPages(menuId);
		return pages;
	}



	
	@Override
	@Transactional
	public void updateDesignTemplate(String template) {
		website.getDesign().setTemplate(template);
		website.getDesign().setColorPalette("");
		cfg.setData(myProps,website);
		dao.update(website.getDesign());  
	}
	

	


	@Override
	@Transactional
	public void updateDesignPalette(String palette) {
		website.getDesign().setColorPalette(palette);
		website.getDesign().setTheme("");
		cfg.setData(myProps,website);
		dao.update(website.getDesign());	
	}
	
	@Override
	@Transactional
	public void updateDesignTheme(String theme) {
        website.getDesign().setTheme(theme); 
        website.getDesign().setBgThemeType(Design.BgTheme.PATTERN);
        dao.update(website.getDesign());      
	}


	@Override
	@Transactional
	public void getModel(long websiteId) {
		Website wbs  = websiteDAO.getWebsiteById(websiteId);
		website.setDesign(wbs.getDesign());
		website.setId(wbs.getId());
		website.setUser(wbs.getUser());
		website.setMenu(wbs.getMenu());
		website.setSocialFeed(wbs.getSocialFeed());
		website.setDomain(wbs.getDomain());
		website.setHeader(wbs.getHeader());
		
		cfg.setData(myProps,website);
		dao.update(website.getDesign());
	}


	@Override
	@Transactional
	public void updateDesignThemeType(String bgtype) {
		int index = 0;
		try{
		    index = Integer.parseInt(bgtype);
		}catch(NumberFormatException nfe){
			nfe.printStackTrace();
		}
		switch (index){
		case 0:
			website.getDesign().setBgThemeType(Design.BgTheme.NONE);
			break;
		case 1:
			website.getDesign().setBgThemeType(Design.BgTheme.PATTERN);
			break;
		case 2:
			website.getDesign().setBgThemeType(Design.BgTheme.UPLOAD_IMAGE);
			break;
		}
		
		dao.update(website.getDesign());
		
	}
	
	

	@Override
	@Transactional
	public void updateHeader(String name) {
		website.getHeader().setName(name);
		dao.update(website.getHeader());
	}

	@Override
	@Transactional
	public void updateSocial(SocialFeed socialFeed) {
		website.getSocialFeed().setTitle(socialFeed.getTitle());
		website.getSocialFeed().setDescription(socialFeed.getDescription());
		website.getSocialFeed().setFacebookId(socialFeed.getFacebookId());
		website.getSocialFeed().setTwitterId(socialFeed.getTwitterId());
		website.getSocialFeed().setYoutubeId(socialFeed.getYoutubeId());
		website.getSocialFeed().setVimeoId(socialFeed.getVimeoId());
		website.getSocialFeed().setKeywords(socialFeed.getKeywords());
		dao.update(website.getSocialFeed());
		
	}





	


	


}
